﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_ProductsPic.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_ProductsPic*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_ProductsPic
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_ProductsPic obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "ProductsPic ([SmallPic],[BigPic],[Pic],[OriginalPic],[ProductID])");
            sql.Append("values(@SmallPic,@BigPic,@Pic,@OriginalPic,@ProductID)");
            sql.Append(";SELECT @@IDENTITY");
            SqlParameter[] pra = {
                            new SqlParameter("@SmallPic",obj.SmallPic),
							new SqlParameter("@BigPic",obj.BigPic),
							new SqlParameter("@Pic",obj.Pic),
                            new SqlParameter("@OriginalPic",obj.OriginalPic),
							new SqlParameter("@ProductID",obj.ProductID)
                                 };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));
        }

        public int Delete(Model.CNVP_ProductsPic obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "ProductsPic where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public Model.CNVP_ProductsPic LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [SmallPic],[BigPic],[Pic],[OriginalPic],[ProductID] from " + Prefix + "ProductsPic");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_ProductsPic cnvp_productspic = new Model.CNVP_ProductsPic();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_productspic.SmallPic = dr["SmallPic"] is DBNull ? String.Empty : (string)dr["SmallPic"];
                cnvp_productspic.BigPic = dr["BigPic"] is DBNull ? String.Empty : (string)dr["BigPic"];
                cnvp_productspic.Pic = dr["Pic"] is DBNull ? String.Empty : (string)dr["Pic"];
                cnvp_productspic.OriginalPic = dr["OriginalPic"] is DBNull ? String.Empty : (string)dr["OriginalPic"];
                cnvp_productspic.ProductID = dr["ProductID"] is DBNull ? 0 : (int)dr["ProductID"];
            }     
             return cnvp_productspic;
        }

        #endregion

        public int DeleteByProductID(int ProductID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from " + Prefix + "ProductsPic where [ProductID] = @ProductID");
            SqlParameter[] pra = {
                                new SqlParameter("@ProductID",ProductID)
						
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public List<Model.CNVP_ProductsPic> LoadAll(int ProductID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[SmallPic],[BigPic],[Pic],[OriginalPic] from " + Prefix + "ProductsPic");
            sql.Append(" where [ProductID] = @ProductID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ProductID",ProductID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            List<Model.CNVP_ProductsPic> list = new List<Model.CNVP_ProductsPic>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_ProductsPic cnvp_productspic = new Model.CNVP_ProductsPic();
                    string XX = dr["ID"].ToString();
                    cnvp_productspic.ID = int.Parse(dr["ID"].ToString());
                    cnvp_productspic.SmallPic = dr["SmallPic"] is DBNull ? String.Empty : (string)dr["SmallPic"];
                    cnvp_productspic.BigPic = dr["BigPic"] is DBNull ? String.Empty : (string)dr["BigPic"];
                    cnvp_productspic.Pic = dr["Pic"] is DBNull ? String.Empty : (string)dr["Pic"];
                    cnvp_productspic.OriginalPic = dr["OriginalPic"] is DBNull ? String.Empty : (string)dr["OriginalPic"];
                    list.Add(cnvp_productspic);
                }
            }
            return list;
        }
        public DataTable LoadProductPicByID(int ProductID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [SmallPic],[BigPic],[Pic],[OriginalPic] from " + Prefix + "ProductsPic");
            sql.Append(" where [ProductID] = @ProductID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ProductID",ProductID)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(), pra);
            return DT;
        }
        public int DeleteNotIn(int ProductID, string picnowid)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from " + Prefix + "ProductsPic where [ProductID]=" + ProductID + " and [ID] not in (" + picnowid + ")");
            return DbHelper.ExecuteNonQuery(sql.ToString());
        }
    }
}

